package cn.bytecollege.user.filter;

import cn.bytecollege.common.constant.Resource;
import cn.bytecollege.common.entity.Address;
import cn.bytecollege.common.entity.User;
import cn.bytecollege.user.service.AddressService;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@WebFilter(filterName = "AddressFilter",servletNames = {"AddressSaveController","AddressUpdateController"})
public class AddressFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        AddressService addressService=new AddressService();
        HttpServletRequest req=(HttpServletRequest)request;
        String method =req.getMethod();
        if(method.equalsIgnoreCase("post")){
            String msg=null;
            String addressConsignee=request.getParameter("Consignee");
            String addressPhonenumber=request.getParameter("Phonenumber");
            String addressProvince=request.getParameter("province");
            String addressCity=request.getParameter("city");
            String addressCounty=request.getParameter("county");
            String addressProfile=request.getParameter("Profile");
            String addressId=request.getParameter("addressId");
            String uri=req.getRequestURI();
            HttpSession session= req.getSession();
            User user=(User) session.getAttribute("user");
            List<Address> lists=addressService.findlistById(user.getUserId());
            if(uri.contains("addAddress")) {
                if (addressConsignee.length() == 0 && addressConsignee.equals("")) {
                    msg = "请输入收货人！";
                    request.setAttribute("msg", msg);
                    request.setAttribute("lists",lists);
                    request.getRequestDispatcher("/addAddress.jsp").forward(request, response);
                    return;
                } else if (addressPhonenumber.length() == 0 && addressPhonenumber.equals("")) {
                    msg = "请输入收货号码！";
                    request.setAttribute("msg", msg);
                    request.setAttribute("lists",lists);
                    request.getRequestDispatcher("/addAddress.jsp").forward(request, response);
                    return;
                } else if (addressProvince.equals("省份")) {
                    msg = "请选择收货所在的省！";
                    request.setAttribute("msg", msg);
                    request.setAttribute("lists",lists);
                    request.getRequestDispatcher("/addAddress.jsp").forward(request, response);
                    return;
                } else if (addressCity.equals("地级市")) {
                    msg = "请选择收货所在的市!";
                    request.setAttribute("msg", msg);
                    request.setAttribute("lists",lists);
                    request.getRequestDispatcher("/addAddress.jsp").forward(request, response);
                    return;
                } else if (!(addressProvince.equals("北京") && addressCity.equals("北京市")) && addressCounty.equals("市、县级市、县")) {
                    msg = "请选择收货所在的县!";
                    request.setAttribute("msg", msg);
                    request.setAttribute("lists",lists);
                    request.getRequestDispatcher("/addAddress.jsp").forward(request, response);
                    return;
                } else if (addressProfile.length() == 0 && addressProfile.equals("")) {
                    msg = "请输入详细信息!";
                    request.setAttribute("msg", msg);
                    request.setAttribute("lists",lists);
                    request.getRequestDispatcher("/addAddress.jsp").forward(request, response);
                    return;
                }
                Pattern pattern = Pattern.compile("^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$");
                Matcher addressPhonenumbermatcher = pattern.matcher(addressPhonenumber);
                if (!addressPhonenumbermatcher.matches()) {
                    msg = "请输入正确的手机号码格式";
                    request.setAttribute("msg", msg);
                    request.setAttribute("lists",lists);
                    request.getRequestDispatcher("/addAddress.jsp").forward(request, response);
                    return;
                }
                chain.doFilter(request, response);
                return;
            }else if(uri.contains("addressUpdate")){
                Address address=new Address();
                address.setAddressConsignee(addressConsignee);
                address.setAddressProvince(addressProvince);
                address.setAddressPhonenumber(addressPhonenumber);
                address.setAddressCity(addressCity);
                address.setAddressCounty(addressCounty);
                address.setAddressProfile(addressProfile);
                address.setAddressId(Integer.valueOf(addressId));
                if (addressConsignee.length() == 0 && addressConsignee.equals("")) {
                    msg = "请输入收货人！";
                    request.setAttribute("msg", msg);
                    request.setAttribute("address",address);
                    request.getRequestDispatcher("/updateAddress.jsp").forward(request, response);
                    return;
                } else if (addressPhonenumber.length() == 0 && addressPhonenumber.equals("")) {
                    msg = "请输入收货号码！";
                    request.setAttribute("msg", msg);
                    request.setAttribute("address",address);
                    request.getRequestDispatcher("/updateAddress.jsp").forward(request, response);
                    return;
                } else if (addressProvince.equals("省份")) {
                    msg = "请选择收货所在的省！";
                    request.setAttribute("msg", msg);
                    request.setAttribute("address",address);
                    request.getRequestDispatcher("/updateAddress.jsp").forward(request, response);
                    return;
                } else if (addressCity.equals("地级市")) {
                    msg = "请选择收货所在的市!";
                    request.setAttribute("msg", msg);
                    request.setAttribute("address",address);
                    request.getRequestDispatcher("/updateAddress.jsp").forward(request, response);
                    return;
                } else if (!(addressProvince.equals("北京") && addressCity.equals("北京市")) && addressCounty.equals("市、县级市、县")) {
                    msg = "请选择收货所在的县!";
                    request.setAttribute("msg", msg);
                    request.setAttribute("address",address);
                    request.getRequestDispatcher("/updateAddress.jsp").forward(request, response);
                    return;
                } else if (addressProfile.length() == 0 && addressProfile.equals("")) {
                    msg = "请输入详细信息!";
                    request.setAttribute("msg", msg);
                    request.setAttribute("address",address);
                    request.getRequestDispatcher("/updateAddress.jsp").forward(request, response);
                    return;
                }
                Pattern pattern = Pattern.compile("^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$");
                Matcher addressPhonenumbermatcher = pattern.matcher(addressPhonenumber);
                if (!addressPhonenumbermatcher.matches()) {
                    msg = "请输入正确的手机号码格式";
                    request.setAttribute("msg", msg);
                    request.setAttribute("address",address);
                    request.getRequestDispatcher("/updateAddress.jsp").forward(request, response);
                    return;
                }
                chain.doFilter(request, response);
                return;
            }
        }
       chain.doFilter(request,response);

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
